Skip to content

feat(openapi-fetch): enable middleware request param module augmentation#2527

Merged
drwpow merged 1 commit intoopenapi-ts:mainfrom
Katli95:main
Feb 8, 2026
Merged

feat(openapi-fetch): enable middleware request param module augmentation#2527
drwpow merged 1 commit intoopenapi-ts:mainfrom
Katli95:main

Conversation

@Katli95
Copy link
Contributor

@Katli95 Katli95 commented Nov 21, 2025

Changes

Decided to open PR in lieu of issue.

I just changed the .d.ts file manually and extracted the MiddlewareRequestParams into their own interface. It opens up the possibility for using TS module augmentation to add parameters to the params property and use those in the middleware.

My use-case is changing authentication logic based on a variable that isn't passed through to the API being invoked.

How to Review

Look at the file, I guess? 🤗

Also, I would be open to going deeper in a follow-up PR to look into how the client facing types could mirror the custom parameter added, if this is indeed an avenue you're open to exploring.

Checklist

(skipped all since it's just a small TS update and I didn't pull to local)

  • Unit tests updated
  • docs/ updated (if necessary)
  • pnpm run update:examples run (only applicable for openapi-typescript)

@Katli95 Katli95 requested a review from a team as a code owner November 21, 2025 10:00
@Katli95 Katli95 requested a review from htunnicliff November 21, 2025 10:00
@netlify
Copy link

netlify bot commented Nov 21, 2025

👷 Deploy request for openapi-ts pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 1671f6f

@changeset-bot
Copy link

changeset-bot bot commented Nov 21, 2025

⚠️ No Changeset found

Latest commit: 1671f6f

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

@drwpow drwpow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense! Little PRs like this that don’t fundamentally change things, just provide little cleanups like this are always fine. Thank you!

@drwpow drwpow merged commit ed0c26c into openapi-ts:main Feb 8, 2026
3 of 7 checks passed
@sk22
Copy link

sk22 commented Mar 5, 2026

It opens up the possibility for using TS module augmentation to add parameters to the params property and use those in the middleware.

Wait, I can't seem to fully get this working...

declare module 'openapi-fetch' {
	interface MiddlewareRequestParams {
		timeout?: number;
	}
}

I can write a timeout middleware like this:

// Timeout middleware
fetchClient.use({
	async onRequest({ request, params: { timeout } }) {
		const signal =
			timeout === Infinity ?
				request.signal
			:	AbortSignal.any([
					request.signal,
					AbortSignal.timeout(typeof timeout === 'number' && timeout >= 0 ? timeout : REQUEST_TIMEOUT),
				]);
		return new Request(request, { signal });
	},
});

But I can't really use my custom parameter because TypeScript will yell at me if I try to do this:

client['/api/takes-a-while'].PUT({ params: { timeout: 120_000 } })

Object literal may only specify known properties, and timeout does not exist in type
{ query?: undefined; header?: undefined; path?: undefined; cookie?: undefined; }. (ts 2353)

Did you somehow get around this without telling TypeScript to ignore this line (or was this just not your use case)?

@Katli95
Copy link
Contributor Author

Katli95 commented Mar 5, 2026

Did you somehow get around this without telling TypeScript to ignore this line (or was this just not your use case)?

Nooo, I was working on it in November and I had to circumvent it 😅 I never found a way to require the param on the calling side.

I used the client.GET(url, {timeout: 20}) syntax. There timeout was allowed but not required, should be simple enough but I don't have the time at the moment.

@sk22
Copy link

sk22 commented Mar 5, 2026

I used the client.GET(url, {timeout: 20}) syntax.

i was about to comment "unfortunately, custom options aren't passed along to middleware" - but it seems like they do, but apparently they're passed directly into the request :') that would work, thanks for your reply!

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants